package data;

import java.util.ArrayList;
import java.util.HashMap;

import org.jdom.DataConversionException;
import org.jdom.Element;

import fileIO.EmployeeInfoWriter;
import fileIO.XMLParser;

import storage.Employee;

/**
 * Retrieves employee data from stored XML files on launch
 * @author lax116
 */
public class EmployeeInfo {
	private static HashMap<String,ArrayList<Employee>> employeeMap;
	public static HashMap<String, Boolean> locationMap; //Stores Locations mapped to origin or destination
	private static HashMap<String, ArrayList<Employee>> salesMap;
	
	public static void initialize(){
		employeeMap = new HashMap<String, ArrayList<Employee>>();
		salesMap = new HashMap<String, ArrayList<Employee>>();
		locationMap = new HashMap<String, Boolean>();
		
		Element root = null;
		root = XMLParser.parseDocument(FilePath.CS_LIST);
		if(root==null){
//			JOptionPane.showMessageDialog(null, "CS List could not be opened at: " + FilePath.CS_LIST +".  "+
//					"Create an employee to save a new file");
		}else{
			parseList(root);
		}
	}
	public static HashMap<String, ArrayList<Employee>> getEmployeeMap(){
		return employeeMap;
	}
	public static HashMap<String,ArrayList<Employee>> getSalesMap(){
		return salesMap;
	}
	private static void parseList(Element root){
		Object[] childList = root.getChildren().toArray();
		//Iterate through list of locations
		for(int i =0;i<childList.length;i++){
			Element currElement = (Element)childList[i];
			String location = currElement.getAttribute("name").getValue().toUpperCase();
			try {
				locationMap.put(location, currElement.getAttribute("origin").getBooleanValue());
			} catch (DataConversionException e) {
				System.out.println("boolean conversion failed");
				e.printStackTrace();
			}
			if(!employeeMap.containsKey(location)){
				employeeMap.put(location, new ArrayList<Employee>());
			}
			ArrayList<Employee> currEmpList =employeeMap.get(location);
			Object[] empNodeList = currElement.getChildren().toArray();
			
			for(int z =0;z<empNodeList.length;z++){
				Element currEmployee = (Element)empNodeList[z];
				String name = currEmployee.getChildText("Name").toUpperCase();
				String email = currEmployee.getChildText("Email").toUpperCase();
				
				currEmpList.add(new Employee(name,email));
			}
		}
	}
	/**
	 * Writes all currently stored data to XML
	 */
	public static void writeToFile(){
		EmployeeInfoWriter.write(employeeMap, locationMap, EmployeeInfoWriter.CS);
	}
	/**
	 * Returns all location names
	 * @return Object[] array of location names (Strings)
	 */
	public static Object[] getLocations(){
		return employeeMap.keySet().toArray();
	}
	/**
	 * Returns all employees at a given location
	 * @param location of office
	 * @return An arraylist of all employee information
	 */
	public static ArrayList<Employee> getEmployees(String location){
		return employeeMap.get(location);
	}
	/**
	 * Returns a list of employee names only by location
	 * @param location Office of employees
	 * @return All employees at given office
	 */
	public static ArrayList<String> getEmployeeNames(String location){
		ArrayList<String> temp = new ArrayList<String>();
		ArrayList<Employee> empList = getEmployees(location);
		if(empList!=null)
		{
			for(int i =0;i<empList.size();i++){
				temp.add(empList.get(i).name);
			}
		}
		return temp;
	}
	public static String getEmployeeEmail(String location, String emp){
		String exactLocation = getExactLocation(location);
		ArrayList<Employee> employeeList = employeeMap.get(exactLocation);
		for(int i=0;i<employeeList.size();i++){
			if(employeeList.get(i).name.equals(emp)){
				return employeeList.get(i).email;
			}
		}
		return null;
	}
	public static String getExactLocation(String location){
		String exactLocation = "DEFAULT";
		ArrayList<String> temp = getOriginLocations();
		for (int i=0;i<temp.size();i++){
			if(temp.get(i).contains(location)){
				exactLocation = temp.get(i);
				break;
			}			
		}
		return exactLocation;
	}
	public static ArrayList<String> getOriginLocations(){
		ArrayList<String> temp = new ArrayList<String>();
		Object[] keySet = getLocations();
		for(int i=0;i<keySet.length;i++){
			if(locationMap.get(keySet[i])){
				temp.add((String)keySet[i]);
			}
		}
		return temp;
	}
	public static ArrayList<String> getDestLocations(){
		ArrayList<String> temp = new ArrayList<String>();
		Object[] keySet = getLocations();
		for(int i=0;i<keySet.length;i++){
			if(!locationMap.get(keySet[i])){
				temp.add((String)keySet[i]);
			}
		}
		return temp;
	}
	public static boolean isOrigin(String location){
		return locationMap.get(location);
	}
	public static HashMap<String, Boolean> getLocationMap(){
		return locationMap;
	}
	@SuppressWarnings("unchecked")
	public static void setEmployeeMap(HashMap<String,ArrayList<Employee>> inputMap){
		employeeMap = (HashMap<String, ArrayList<Employee>>) inputMap.clone();
	}
	@SuppressWarnings("unchecked")
	public static void setLocationMap(HashMap<String,Boolean> inputMap){
		locationMap = (HashMap<String, Boolean>) inputMap.clone();
	}
}
